﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiffenGUI.BiffenService;
using System.Globalization;

namespace BiffenGUI.UserControls
{
    public partial class PlanShowings : UserControl
    {
        PlanShowsTableHelper helper;
        Movie movie;
        private BiffenServiceClient service = null;
        private DateTime dateMonday;
        private int id;

        public PlanShowings()
        {
            InitializeComponent();
        }

        public void SetServiceReferance(BiffenServiceClient svc)
        {
            service = svc;
            service = new BiffenServiceClient();
            movie = new Movie();

            helper = new PlanShowsTableHelper(dataGridView1);
        }

        private void monthCalendar1_DateChanged_1(object sender, DateRangeEventArgs e)
        {
            DateTime date = monthCalendar1.SelectionStart;
            CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;

            DateTime newMonday = GetFirstDayOfWeek(date, defaultCultureInfo);
            //if(!newMonday.Equals(dateMonday)) {
                dateMonday = newMonday;
                textBox2.Text = dateMonday.ToString();
                UpdateShows();
            //}
            
        }

        /// <summary>
        /// Returns the first day of the week that the specified date 
        /// is in. 
        /// </summary>
        public static DateTime GetFirstDayOfWeek(DateTime dayInWeek, CultureInfo cultureInfo)
        {
            DayOfWeek firstDay = cultureInfo.DateTimeFormat.FirstDayOfWeek;
            DateTime firstDayInWeek = dayInWeek.Date;
            while (firstDayInWeek.DayOfWeek != firstDay)
                firstDayInWeek = firstDayInWeek.AddDays(-1);

            return firstDayInWeek;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            helper.SetStringsOfCells();
            List<Show> shows = new List<Show>();
            shows = service.GetCurrentShowsSeven(dateMonday, int.Parse((string)theaterDropDown.SelectedItem));

            helper.SetTable(shows);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox5.Text = helper.GetRows();
            textBox4.Text = helper.GetDayInWeek().ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Movie movie = service.GetMovie(id);

            DateTime date = dateMonday;
            date = date.AddDays(int.Parse(textBox4.Text));
            date = date.AddHours(int.Parse(textBox5.Text) + 15);

            Theater theater = new Theater();
            theater.Id = int.Parse((string)theaterDropDown.SelectedItem);

            List<Movie> movies = new List<Movie>();
            movies.Add(movie);

            Show show = new Show();
            show.Description = "Test Beskrivelse";
            show.Duration = int.Parse(textBox6.Text)*60;
            show.Movies = movies;
            show.StartTime = date;
            show.Theater = theater;

            service.AddShow(show);
        }

        public void LoadCombo()
        {
            List<Movie> movies = service.GetAllMovies();
            comboBox1.DisplayMember = "Title";
            comboBox1.ValueMember = "Id";

            comboBox1.Items.AddRange(movies.ToArray());       
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            id = ((Movie)comboBox1.SelectedItem).Id;
            Movie movie = service.GetMovie(id);

            textBox3.Text = (movie.Duration / 60).ToString();
            textBox6.Text = ((movie.Duration / 60) + 1).ToString();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void UpdateShows()
        {
            List<Show> shows = service.GetCurrentShowsSeven(dateMonday, int.Parse(theaterDropDown.SelectedItem.ToString()));
            helper.SetTable(shows);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Show show = helper.GetSelectedShow(dateMonday);

            if (show != null)
            {
                service.DeleteShow(show);
                UpdateShows();
            }
        }

        private void PlanShowings_Load(object sender, EventArgs e)
        {
            theaterDropDown.SelectedIndex = 0;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox5.Text = helper.GetRows();
            textBox4.Text = helper.GetDayInWeek().ToString();
        }


    }
}
